package com.apesay.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apesay.exception.ServiceException;
import com.apesay.security.CustomAuthenticationException;
import com.apesay.service.auth.AccountService;
import com.apesay.service.auth.PermissionService;
import com.apesay.service.work.ProjectService;
import com.apesay.service.work.TaskService;
import com.apesay.utils.Constant;
import com.apesay.utils.DataUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Enumeration;

@Controller
public class HomeController {
	Logger logger = LoggerFactory.getLogger(HomeController.class);
	@Resource
	private ProjectService projectService;
	@Autowired
	private TaskService taskService;
	@Resource
	AccountService accountService;
	@RequestMapping("/login")
	public String login() {
		return "home/login";
	}
	@RequestMapping("/register")
	public String register() {
		return "home/register";
	}
	@RequiresAuthentication
	@RequestMapping("/kanban/board_list")
	public String dashboard(HttpSession session,Model model) {
		Object userObj = session.getAttribute("currentaccount");//当前登录用户
		JSONObject user = JSON.parseObject(userObj.toString());
		String userId = user.getString(Constant.OBJECTID);
		JSONArray projectList = projectService.listByUserId(userId);
		ArrayList projects = new ArrayList();
		for (Object project:projectList) {
			JSONObject p = (JSONObject) project;
			String objectId = p.getString(Constant.OBJECTID);
			if (DataUtils.isNotEmpty(objectId)) {
				JSONArray tasks = taskService.listByProId(objectId);
				p.put("tasks",tasks);
				projects.add(p);
			}
		}
		model.addAttribute("projectList", projects);
		return "board/dashboard";
	}

	@RequestMapping("/dologin")
	@ResponseBody
	public String dologin(String username, String password, HttpSession session) {
		String msg = "success";
		try {
			SecurityUtils.getSubject().login(new UsernamePasswordToken(username, password));
			Object object = SecurityUtils.getSubject().getPrincipal();
			if (object != null) {
				JSONObject accountObject=accountService.findByName(object.toString());
				session.setAttribute("currentaccount", accountObject);
			}
		} catch (CustomAuthenticationException exception) {
			logger.info("账户校验异常");
			msg = exception.getMessage();
		}
		return msg;
	}

	@SuppressWarnings("rawtypes")
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		Enumeration enumeration = session.getAttributeNames();
		while (enumeration.hasMoreElements()) {
			session.removeAttribute(enumeration.nextElement().toString());
		}
		return "redirect:/login";
	}

	@RequestMapping("/unauthorized")
	public String unauthorized() {
		return "home/unauthorized";
	}

	@RequestMapping("/unauthenticated")
	public String unauthenticated() {
		return "home/unauthenticated";
	}
}
